//
//  GoodsListController.m
//  MindBox
//
//  Created by MS on 16-1-11.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "GoodsListController.h"
#import "GoodsDetailController.h"
#import "GoodsController.h"
@interface GoodsListController ()<GoodsListViewDelegate>

@end

@implementation GoodsListController


-(Param *)param
{
    if (_param==nil) {
        _param = [Param param];
    }
    return _param;
}

-(GoodsListView *)goodsListView
{
    if (_goodsListView==nil) {
        GoodsListView * listView = [[GoodsListView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, self.view.height-64)];
        _goodsListView = listView;
        _goodsListView.selDelegate = self;
        [self.view addSubview:listView];
        
        __weak typeof(self) weakSelf = self;
        [_goodsListView setGoodsListViewClickIndexBlock:^(NSInteger index) {
            
            Gift_ItemGoods * goods = weakSelf.itemGoodsArray[index];
            
            GoodsController * vc  = [[GoodsController alloc] init];
            vc.good_id = goods.goods_id;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
    }
    return _goodsListView;
}

-(NSMutableArray *)itemGoodsArray
{
    if (_itemGoodsArray==nil) {
        _itemGoodsArray = [NSMutableArray array];
    }
    return _itemGoodsArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadGoodsListDataWithParam:[Param param]];
    
    [self addFootRefresh];
    [self addHeaderRefresh];
    
    [self setNavBar];
}



-(void)loadGoodsListDataWithParam:(Param *)param
{
    if (self.category_id) {
        param.category = _category_id;
    }
    else if (self.label_id){
        param.label = _label_id;
    }
    else if (self.name_id){
        param.name = _name_id;
    }
    
    param.page = ++_pages;
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    [GoodsListPageTool googListDataWithParam:param Success:^(NSArray *data) {
         [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [_goodsListView.collectionView.mj_footer endRefreshing];
        [_goodsListView.collectionView.mj_header endRefreshing];
        
        if (data.count) {
            [self.itemGoodsArray addObjectsFromArray:data];
        }
        self.goodsListView.gift_ItemGoods = _itemGoodsArray;
        
    } failure:^(NSError *error) {
        
    }];
    
}

-(void)goodsListViewDidClickSelectGoods:(GoodsListView *)goodsListView param:(Param *)param{
    
    self.param = param;
    self.pages = 0;
    [self.itemGoodsArray removeAllObjects];
    [self loadGoodsListDataWithParam:param];
}


-(void)addFootRefresh
{
    __weak typeof(self) weakSelf = self;
    
    self.goodsListView.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadGoodsListDataWithParam:weakSelf.param];
    }];
    
}

-(void)addHeaderRefresh
{
    __weak typeof(self) weakSelf = self;
    
    self.goodsListView.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pages = 0;
        [weakSelf.itemGoodsArray removeAllObjects];
        [weakSelf loadGoodsListDataWithParam:weakSelf.param];
        
    }];
}

-(void)setNavBar
{
    MyNavigationItem * leftItem = [MyNavigationItem navigationItemWithImage:@"left_go" andHighlightImage:@"left_go" andTag:0 andTitle:nil addTarget:self action:@selector(leftBtnClick)];
    
    MyNavigationItem * rightItem =[MyNavigationItem navigationItemWithImage:@"threedot" andHighlightImage:@"threedot" andTag:0 andTitle:nil addTarget:self action:@selector(rightClick)];
    
    MyNavigationBar * navBar = [[MyNavigationBar alloc]initWithFrame:CGRectMake(0, 20, self.view.width, 44)];
    navBar.backgroundColor = [UIColor whiteColor];
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, navBar.frame.size.height-0.5, navBar.frame.size.width, 0.5)];
    lineView.backgroundColor =[UIColor grayColor];
    [navBar addSubview:lineView];
    self.mynavBar = navBar;
    [navBar setNavigationBarWithBgImageName:@"无" andTitle:self.titleName  andTitleView:nil  andLeftItems:@[leftItem] andRightItems:@[rightItem]];
    [self.view addSubview:navBar];
    
}

-(void)leftBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)rightClick{
    
    
}

-(void)dealloc
{
//    NSLog(@"GoodsListController卸载");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
